{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "96b2b1e4",
   "metadata": {},
   "source": [
    "# Async TreeIndex Demo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9331cfeb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# NOTE: This is ONLY necessary in jupyter notebook.\n",
    "# Details: Jupyter runs an event-loop behind the scenes.\n",
    "#          This results in nested event-loops when we start an event-loop to make async queries.\n",
    "#          This is normally not allowed, we use nest_asyncio to allow it for convenience.\n",
    "import nest_asyncio\n",
    "\n",
    "nest_asyncio.apply()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8a1d2821",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "from llama_index import TreeIndex, SimpleDirectoryReader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6948df36",
   "metadata": {},
   "outputs": [],
   "source": [
    "# load documents\n",
    "documents = SimpleDirectoryReader(\"../paul_graham_essay/data\").load_data()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d9115d1",
   "metadata": {},
   "source": [
    "#### By default, TreeIndex makes blocking LLM calls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d9ef0fef",
   "metadata": {},
   "outputs": [],
   "source": [
    "start_time = time.perf_counter()\n",
    "index = TreeIndex.from_documents(documents)\n",
    "elapsed_time = time.perf_counter() - start_time"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9392d573",
   "metadata": {},
   "source": [
    "It takes ~47s to finish building TreeIndex from 5 text chunks."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "474f82d1",
   "metadata": {},
   "source": [
    "#### Pass in `use_async=True` to enable asynchronous LLM calls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "78a02987",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "start_time = time.perf_counter()\n",
    "index = TreeIndex.from_documents(documents, use_async=True)\n",
    "elapsed_time = time.perf_counter() - start_time"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23469128",
   "metadata": {},
   "source": [
    "It takes ~12s to finish building the TreeIndex from 5 text chunks."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "59c1c27e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}